<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>


<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.memcache.MemcacheService" %>
<%@ page import="com.google.appengine.api.memcache.MemcacheServiceFactory" %>

<%@ page import="javax.jdo.Query" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>

<%@ page import="connectfour.PMF" %>
<%@ page import="connectfour.Game" %>
<%@ page import="connectfour.Player" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>QUAD ATTACK! - Leaderboard</title>
</head>
<body>
	<center>
		<h1>Leaderboard</h1>
		<h2>(By Win/Loss Ratio)</h2><br />
		<a href="menu.jsp">MAIN MENU</a>
	</center>
	
		<% 
			MemcacheService mc = MemcacheServiceFactory.getMemcacheService();
		
		
			int count = 0;
			PersistenceManager pm = PMF.get().getPersistenceManager();
		
			List<Player> players = null;
			if(mc.contains("TopFive")){
				players = (List<Player>) mc.get("TopFive");
			}
			else{
				String query = "SELECT FROM connectfour.Player ORDER BY ratio DESC LIMIT 5";
		    	players = (List<Player>) pm.newQuery(query).execute();
		    	mc.put("TopFive", new ArrayList<Player>(players));
			}
	    	%>
	    
	    <% if(players.isEmpty()){ %>
			<p>There no players in the database</p>
		<% } else{ %>
			<table>
			<% for(Player p:players){ %>
				<%count++;%>
				<td><%=count%> [Name: <%=p.getPlayer()%>]  </td>
				<td>[Wins: <%=p.getWins()%>]  </td>
				<td>[Losses: <%=p.getLosses()%>]  </td>
				<td>[W/L Ratio: <%=p.getRatio() %>]  </td>
				<td>[Last Game Played: <%=p.getLastGamePlayed()%>]</td>
				</tr>
			<% } %>
			</table>
		<% } %>
	


</body>
</html>